package plantshop.model;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Serializable;
import java.net.Socket;
import java.util.List;
import java.util.Map;

import javax.swing.JFrame;

import plantshop.server.ClientCommunicateThread;


public class Client implements Serializable{
	private static final long serialVersionUID = -6560882325907167395L;
	private PrintWriter outputWriter;
	private BufferedReader inputReader;
	
	private final static int PORT = 9876;
	
	private Player player;
	
	private Socket socket;
	
	private ClientCommunicateThread thread;
	
	public Client(String IP, String name, boolean gender, Map<String, BufferedImage> images, JFrame frame, Map<String, BufferedImage[]> animations) throws IOException{
		this.socket = new Socket(IP, PORT);
		outputWriter = new PrintWriter(socket.getOutputStream(), true);
		inputReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		this.player = new Player(name, gender, images, frame, animations);
		thread = new ClientCommunicateThread(this);
		thread.start();
	}
	
	public Client(Socket socket) throws IOException{
		this.socket = socket;
		outputWriter = new PrintWriter(socket.getOutputStream(), true);
		inputReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

		player = new Player();
	}
	
	public int getPlayerScore(){
		return player.getStoreAttractiveness();
	}
	
	public int getPlayerMoney(){
		return player.getMoney();
	}
	
	public String getGender(){
		return Boolean.toString(player.getGender());
	}
	
	public List<Opponent> getOpponents(){
		return player.getOpponents();
	}
	
	public String getName(){
		return player.getName();
	}
	
	public void setGender(boolean gender){
		player.setGender(gender);
	}
	
	public void setName(String name){
		player.setName(name);
	}
	
	public void setScore(int score){
		player.setScore(score);
	}
	
	public void setMoney(int money){
		player.setMoney(money);
	}
	
	public void addOpponent(boolean gender, String name){
		player.addOpponent(gender, name);
	}
	
	public Socket getSocket(){
		return socket;
	}
	
	public void write(int write) throws IOException{
		write(Integer.toString(write));
	}
	public void write(String write) throws IOException{
		outputWriter.println(write);
	}
	
	public void updateTotal(){
		player.updateTotalScore();
	}
	
	public String read() throws IOException{
		return inputReader.readLine();
	}
	
	public boolean hasNext() throws IOException{
		return inputReader.ready();
	}
	
	public void startGame(){
		player.startGame();
	}
}
